/*
 * Copyright (c) 2020, 2025, keyila All rights reserved.
 *
 */
package com.clp;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

/**
 * @author clp
 * @version 1.0
 */

@Slf4j
@Component
@Aspect
@ConditionalOnProperty(name = {"log.aspect.enable"},havingValue = "true",matchIfMissing = true)
public class LogAspect {

    @Pointcut("execution(* com.clp.*.controller.*Controller.*(..))||execution(* com.clp.*.service.*Service.*(..))")
    private void pointCut(){
    }

    @Around("pointCut()")
    public Object around (ProceedingJoinPoint pjq) throws Throwable {
        Object[] reqArgs = pjq.getArgs();
        String req = new Gson().toJson(reqArgs);
        MethodSignature methodSignature = (MethodSignature) pjq.getSignature();
        String methodName=methodSignature.getDeclaringType().getName()+"."+methodSignature.getName();
        log.info("{},req:{}",methodName,req);
        Long startTime=System.currentTimeMillis();
        Object responseObj = pjq.proceed();
        String response = new Gson().toJson(responseObj);
        Long endTime=System.currentTimeMillis();
        log.info("{},response:{},costTime:{}",methodName,response,endTime-startTime);
        return responseObj;
    }

}
